package fractal;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import entity.Position;

public class FractalView extends BasicGame
{
	protected double[][] _map;
	protected boolean[][] _waterMap;
	protected Image _niv1;
	protected Image _niv2;
	protected Image _niv3;
	protected Image _niv4;
	protected Image _niv5;
	protected Image _sep;
	protected Image _water;
	protected Position _pos;
	protected static int NBSCREEN = 512;

	public FractalView(String title)
	{
		super(title);
	}

	public void calculate() {
		//_map = GenerateurFractal.getFractal("MERVCETEMP", 9);

		_waterMap = GenerateurRiviere.river(_map);
	}

	@Override
	public void render(GameContainer arg0, Graphics arg1) throws SlickException {
		// TODO Auto-generated method stub
		for (int i = _pos.getX(); i < _pos.getX() + NBSCREEN; i++)
		{
			for (int j = _pos.getY(); j < _pos.getY() + NBSCREEN; j++)
			{
				if (_waterMap[i][j])
				{
					_water.draw(i * _water.getWidth(), j * _water.getHeight());
				}
				else if (_map[i][j] < 40)
				{
					 _niv1.draw(i * _niv1.getWidth(), j * _niv1.getHeight());
					//_water.draw(i * _water.getWidth(), j * _water.getHeight());
				}
//				else if (_map[i][j] < 80)
//				{
//					_niv2.draw(i * _niv2.getWidth(), j * _niv2.getHeight());
//				}
//				else if (_map[i][j] < 100)
//				{
//					_niv3.draw(i * _niv3.getWidth(), j * _niv3.getHeight());
//				}
//				else if (_map[i][j] < 130)
//				{
//					_niv4.draw(i * _niv4.getWidth(), j * _niv4.getHeight());
//				}
				else
				{
					_niv5.draw(i * _niv5.getWidth(), j * _niv5.getHeight());
				}
			}

		}
	}

	@Override
	public void init(GameContainer pGC) throws SlickException {
		// TODO Auto-generated method stub
		_niv1 = new Image("data/niv1.bmp");
		_niv2 = new Image("data/niv2.bmp");
		_niv3 = new Image("data/niv3.bmp");
		_niv4 = new Image("data/niv4.bmp");
		_niv5 = new Image("data/niv5.bmp");
		_water = new Image("data/water.bmp");
		_sep = new Image("data/sep.bmp");
		calculate();
		_pos = new Position(0, 0);
		//
		// pGC.setMaximumLogicUpdateInterval(60);
		// pGC.setMinimumLogicUpdateInterval(interval)
	}

	@Override
	public void update(GameContainer gc, int arg1) throws SlickException {
		Input input = gc.getInput();
		if (input.isKeyPressed(Input.KEY_A))
		{
			calculate();
		}
		if (input.isKeyDown(Input.KEY_Z) && _pos.getY() - 1 >= 0)
		{
			_pos = new Position(_pos.getX(), _pos.getY() - 1);
		}
		if (input.isKeyDown(Input.KEY_S)
				&& _pos.getY() + 1 < _map.length - NBSCREEN)
		{
			_pos = new Position(_pos.getX(), _pos.getY() + 1);
		}
		if (input.isKeyDown(Input.KEY_Q) && _pos.getX() - 1 >= 0)
		{
			_pos = new Position(_pos.getX() - 1, _pos.getY());
		}
		if (input.isKeyDown(Input.KEY_D)
				&& _pos.getX() + 1 < _map.length - NBSCREEN)
		{
			_pos = new Position(_pos.getX() + 1, _pos.getY());
		}
	}

}
